<template>
  <el-card>
    <el-form inline>
      <el-form-item>
        <el-input placeholder="用户名" v-model="roleName" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :icon="Search">查询</el-button>
        <el-button>清空</el-button>
      </el-form-item>
    </el-form>
    <el-button type="primary" @click="showAddRole">添加</el-button>
    <el-button type="danger" disabled>批量删除</el-button>
    <el-table :data="roleList" border class="mt-20">
      <el-table-column
        type="selection"
        width="60"
        align="center"
      ></el-table-column>
      <el-table-column
        type="index"
        label="序号"
        width="80"
        align="center"
      ></el-table-column>
      <el-table-column label="角色名称" prop="roleName" />
      <el-table-column label="操作" width="180">
        <template v-slot="{ row }">
          <el-button
            type="info"
            :icon="User"
            size="small"
            @click="showRoleAuth(row)"
          ></el-button>
          <el-button type="warning" :icon="Edit" size="small"></el-button>
          <el-button type="danger" :icon="Delete" size="small"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[5, 10, 15, 20]"
      layout=" prev, pager, next, jumper,->,sizes,total"
      :total="total"
      @size-change="getRoleList"
      @current-change="getRoleList"
      class="mt-20"
    />
    <el-dialog v-model="visible" title="添加角色">
      <el-form :model="roleFormData">
        <el-form-item label="角色名称">
          <el-input placeholder="用户名" v-model="roleFormData.roleName" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div>
          <el-button type="primary" @click="addRole">保存</el-button>
          <el-button @click="visible = false">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </el-card>
</template>

<script lang="ts">
export default {
  name: "XRole",
};
</script>

<script lang="ts" setup>
import { Search, Edit, Delete, User } from "@element-plus/icons-vue";
import { ref, onMounted, reactive } from "vue";
import { getRoleListApi, addRoleApi } from "@/api/acl/role";
import type { RolesList, RolesItem } from "@/api/acl/type/userType";
import { useRouter } from "vue-router";
const router = useRouter();
const showRoleAuth = (row: RolesItem) => {
  console.log(row);

  router.push({
    name: "RoleAuth",
    query: {
      id: row.id,
      roleName: row.roleName,
    },
  });
};
/*******************添加角色************************** */
const visible = ref(false);
const roleFormData = reactive({
  roleName: "",
});
const showAddRole = () => {
  visible.value = true;
  roleFormData.roleName = "";
};
const addRole = async () => {
  await addRoleApi(roleFormData);
  getRoleList();
  visible.value = false;
};
/*****************获取角色列表******************** */
const currentPage = ref(1);
const pageSize = ref(5);
const roleName = ref("");
const total = ref(100);
const roleList = ref<RolesList>([]);
const getRoleList = async () => {
  const res = await getRoleListApi({
    page: currentPage.value,
    limit: pageSize.value,
    roleName: roleName.value,
  });
  total.value = res.total;
  roleList.value = res.items;
};
onMounted(() => {
  getRoleList();
});
</script>

<style scoped></style>
